package com.nhim.cms.dao;

// default package

import java.util.List;

import javax.annotation.PostConstruct;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.PersistenceException;
import javax.persistence.Query;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.orm.jpa.JpaCallback;
import org.springframework.stereotype.Repository;

import com.nhim.cms.dao.IElementTemplateDAO;
import com.nhim.cms.domain.ElementTemplate;
import com.nhim.core.dao.CoreDAO;

/**
 * A data access object (DAO) providing persistence and search support for
 * ElementTemplate entities. Transaction control of the save(), update() and
 * delete() operations can directly support Spring container-managed
 * transactions or they can be augmented to handle user-managed Spring
 * transactions. Each of these methods provides additional information for how
 * to configure it for the desired type of transaction control.
 * 
 * @see .ElementTemplate
 * @author Huy Lieu Ta
 */
@Repository("elementTemplateDAO")
public class ElementTemplateDAO extends CoreDAO<Integer, ElementTemplate>
		implements IElementTemplateDAO {
	// property constants
	public static final String EMPTY_TEXT = "emptyText";
	public static final String FOOTER_TEXT = "footerText";
	public static final String HEADER_TEXT = "headerText";
	public static final String NAME = "name";
	public static final String NORMAL_TEXT = "normalText";
	public static final String ELEMENT_DEFINITION = "elementDefinition";

	@Autowired
	EntityManagerFactory entityManagerFactory;

	@PostConstruct
	public void init() {
		super.setEntityManagerFactory(entityManagerFactory);
	}

	@SuppressWarnings({ "unchecked", "rawtypes" })
	@Override
	public List<ElementTemplate> findByProperty(String propertyName,
			final Object value) {
		try {
			final String queryString = "select model from ElementTemplate model where model."
					+ propertyName + "= :propertyValue";
			return getJpaTemplate().executeFind(new JpaCallback() {
				public Object doInJpa(EntityManager em)
						throws PersistenceException {
					Query query = em.createQuery(queryString);
					query.setParameter("propertyValue", value);
					return query.getResultList();
				}
			});
		} catch (RuntimeException re) {
			logger.error("find by property name failed", re);
			return null;
		}
	}

	public static IElementTemplateDAO getFromApplicationContext(
			ApplicationContext ctx) {
		return (IElementTemplateDAO) ctx.getBean("elementTemplateDAO");
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<ElementTemplate> findByElementDefinitionId(final Integer id) {
		try {
			final String queryString = "select model from ElementTemplate model where model.elementDefinition.id"
					+ "= :propertyValue";
			return getJpaTemplate().executeFind(new JpaCallback<Object>() {
				public Object doInJpa(EntityManager em)
						throws PersistenceException {
					Query query = em.createQuery(queryString);
					query.setParameter("propertyValue", id);
					return query.getResultList();
				}
			});
		} catch (RuntimeException re) {
			logger.error("find by ElementDefinition Id failed", re);
			return null;
		}
	}

	@Override
	public ElementTemplate findByNameAndElementDefinitionId(final Integer id,
			final String name) {
		try {
			final String queryString = "select model from ElementTemplate model where model.elementDefinition.id"
					+ "= ?1 AND model.name= ?2";
			List<?> result = getJpaTemplate().find(queryString, id,name);
			if(result.isEmpty())
				return null;
			else
				return (ElementTemplate) result.get(0);
			
		} catch (RuntimeException re) {
			logger.error("findByNameAndElementDefinitionId(Integer id,String name) failed", re);
			return null;
		}
	}
}